/*
   Updated by plo, 11/11/2011:
   
   Use these classes:
   - OppForNatSalesHelperClass as the helper class
   - OpportunitySharkPoolTriggerTester as the unit test class
*/
trigger ContactTrigger on Contact (before insert, before update, before delete) 
{
	Contact oldObj;
	Contact newObj;
	
	if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) 
	{		
		for (Integer i = 0; i < Trigger.new.size(); i++)		
		{
			oldObj = Trigger.isUpdate ? Trigger.old[i] : null;
			newObj = Trigger.new[i];

			ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Contact', 'Local Advertiser');
		
			if (localAdvertiserRecordTypeID == newObj.RecordTypeId)
			{
				if (Trigger.isInsert || (Trigger.isUpdate && oldObj.Phone != newObj.Phone)) // Disable this line for first time inital load 
					newObj.CleanPhoneText__c = SilCustomerHelper.CreatePhoneNumberHash(newObj.Phone);
			}			
		}	
	}
	
	if(Trigger.isBefore && Trigger.isDelete) 
	{		
		for (Integer i = 0; i < Trigger.old.size(); i++)		
		{
			oldObj = Trigger.old[i];
			
			if(oldObj.api_controlled__c && !userinfo.getUserName().startsWith('sadmin@ingenio.com'))
				oldObj.addError('API Controlled Contacts cannot be deleted.');
		}
	}
	
	/* 
	   AD-26872, 1/11/2012
	   For now, let's exclude the following from the Validation Rule:
      Changes made by sadmin, If the email address is EmailNotValid@attinteractive.com
	*/
	User sadmin = [SELECT Id FROM User WHERE Alias = 'sadmin' LIMIT 1];
	
	if(UserInfo.getUserId() != sadmin.Id)
	{
		/*	AD-24884, updated by plo, 11/11/2011 */
      OppForNatSalesHelperClass.triggerValidateIOGContacts();
	}
}